package com.sgg.datastructure.doubleLinkedList;

public class DoubleLinkList {
    public HeroNode head = new HeroNode(0, "", "");

    /**
     * 默认加到最后
     *
     * @param heroNode
     */
    public void add(HeroNode heroNode) {
        HeroNode tmp = head;
        while (true) {
            if (tmp.next == null) {
                tmp.next = heroNode;
                heroNode.prev = tmp;//多维护个
                break;
            }
            tmp = tmp.next;
        }
    }

    //作业
    //按照编号no的顺序添加到指定位置
    //编号相同则提示
    public void addByOrder(HeroNode node) {
        boolean flag = false;
        HeroNode temp = head;
        while (true) {
            if (temp.next == null) {
                break;
            }
            if (temp.next.no == node.no) {
                flag = true;
                break;
            } else if (temp.next.no > node.no) {
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            System.out.printf("英雄编号%d已经重复了,不允许重复添加\n", node.no);
        } else {
            if (temp.next != null) {
                temp.next.prev = node;
            }
            node.next = temp.next;
            temp.next = node;
            node.prev = temp;
        }
    }

    //修改，不变
    public void update(HeroNode heroNode) {
        boolean flag = false;
        if (head.next == null) {
            System.out.println("链表为空,无法操作");
            return;
        }
        HeroNode temp = head;
        while (true) {
            temp = temp.next;
            if (temp.no == heroNode.no) {
                flag = true;
                break;
            }
            if (temp.next == null) {
                break;
            }
        }
        if (flag) {
            temp.name = heroNode.name;
            temp.nickname = heroNode.nickname;
        } else {
            System.out.printf("链表里面没有编号为%d的英雄\n", heroNode.no);
        }
    }

    //删除:找到相等的那个节点的 他的上个节点指向他的下个节点，如果有下个节点，那么下个节点指向上个节点
    public void delete(int no) {
        boolean flag = false;
        if (head.next == null) {
            System.out.println("链表为空,无法操作");
            return;
        }
        HeroNode temp = head.next;
        while (true) {
            if (temp == null) {
                break;
            }
            if (temp.no == no) {
                flag = true;
                break;
            }
            temp = temp.next;
        }
        if (flag) {
            temp.prev.next = temp.next;
            if (temp.next != null) {
                temp.next.prev = temp.prev;
            }
        } else {
            System.out.printf("链表里面没有编号为%d的英雄\n", no);
        }
    }

    //不变
    public void show() {
        if (head.next == null) {
            System.out.println("链表无数据");
            return;
        }
        HeroNode tmp = head.next;
        do {
            System.out.println(tmp);
            tmp = tmp.next;
        } while (tmp != null);
    }

    public int getSize() {
        if (head.next == null) {
            return 0;
        }
        HeroNode temp = head.next;
        int count = 0;
        while (temp != null) {
            count++;
            temp = temp.next;
        }
        return count;
    }

    public static void main(String[] args) {
        DoubleLinkList list = new DoubleLinkList();
//        list.add(new HeroNode(1, "宋江", "及时雨"));
//        list.add(new HeroNode(2, "22", "及时雨"));
//        list.add(new HeroNode(3, "吴勇", "智多星"));
//        list.add(new HeroNode(4, "aa", "dddd"));
//        list.show();
//        list.update(new HeroNode(4, "aa4", "dddd4"));
//        System.out.println();
//        list.show();
//        list.delete(4);
//        System.out.println();
//        list.show();
        list.addByOrder(new HeroNode(2, "22", "及时雨"));
        list.addByOrder(new HeroNode(1, "宋江", "及时雨"));
        list.addByOrder(new HeroNode(4, "aa", "dddd"));
        list.addByOrder(new HeroNode(9, "吴勇9", "智多星"));
        list.addByOrder(new HeroNode(8, "吴勇8", "智多星"));
        list.addByOrder(new HeroNode(6, "吴勇6", "智多星"));
        list.show();

    }
}
